//
//  MZAniDemoViewController.m
//  MZAnimationDemo
//
//  Created by Chris Yang on 2016/11/30.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "CAAnimation+MZCategory.h"
#import "MZAniDemoViewModel.h"

#import "MZAniShowView.h"
#import "MZAniBar.h"
#import "MZAniView.h"

#import "MZAniDemoViewController.h"

@interface MZAniDemoViewController ()
<MZAniDemoViewModelDelegate,
MZAniShowViewDelegate,
MZAniBarDelegate>

@property (nonatomic, strong) MZAniDemoViewModel *viewModel;
@property (nonatomic, strong) MZAniView *aniView;

@property (nonatomic) MZAnimationTag curSelectedAni;

@end

@implementation MZAniDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configSelf];
    [self configSubViews];
    
    /* Duration */
    [_aniView.showView updateDuration:_viewModel.animationDuration];
}

- (void)configSelf {
    _viewModel = [[MZAniDemoViewModel alloc] init];
    [_viewModel setDelegate:self];
}

- (void)configSubViews {
    
    _aniView = [[MZAniView alloc] init];
    [_aniView.showView setDelegate:self];
    [_aniView.aniBar resetWithTitleArray:[_viewModel aniTitleArray]];
    [_aniView.aniBar setDelegate:self];
    
    [self.view addSubview:_aniView];
    [_aniView setFrame:self.view.bounds];
}

#pragma mark - MZAniDemoViewModelDelegate

- (void)viewModel:(MZAniDemoViewModel *)model duraitonDidUpdate:(float)duration {
    [_aniView.showView updateDuration:duration];
}

#pragma mark - MZAniShowViewDelegate

- (void)showView:(MZAniShowView *)view buttonClickedWithOpType:(MZAnimationOperationType)opType {
    
    UIView *aniView = view.imageView;
    CALayer *aniLayer = aniView.layer;
    
    if (MZAnimationOperationTypeRestore == opType) {
        
        /* 重置 */
        [view restore];
        
    } else if (MZAnimationOperationTypePerform == opType) {
        
        /* 动画 */
        [_viewModel addAnimationWithView:aniView aniTag:_curSelectedAni];
        
    } else if (MZAnimationOperationTypePause == opType) {
        
        /* 暂停 */
        [CAAnimation mzPauseAnimationWithLayer:aniLayer];
        
    } else if (MZAnimationOperationTypeContinue == opType) {
        
        /* 继续 */
        [CAAnimation mzResumeAnimationWithLayer:aniLayer];
        
    } else if (MZAnimationOperationTypeSetDuration == opType) {
        
        /* 动画时间 */
        UIAlertController *tmpAc = [_viewModel durationSettingActionSheet];
        [self presentViewController:tmpAc animated:YES completion:nil];
    }
    
    NSLog(@"Ani Tag: %ld, Operation Type: %ld", (long)_curSelectedAni, (long)opType);
}

#pragma mark - MZAniBarDelegate

- (void)aniBar:(MZAniBar *)bar itemButtonClickedAtIndex:(NSInteger)index {
    
    [_aniView.aniBar setAllSelected:NO];
    [_aniView.aniBar setSelected:YES atIndex:index];
    _curSelectedAni = MZAnimationTagBase + index + 1;
}

@end

















